import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { DatePipe } from '@angular/common';
import { LocalStorage } from '@core/local.storage';
import { NzMessageService } from 'ng-zorro-antd';
import { environment } from '../../../../../environments/environment';

@Component({
    selector: 'app-controlprocess',
    templateUrl: './process.component.html',
    styleUrls: ['./process.component.less']
})

export class ControlprocessComponent implements OnInit {
    @Input() queryParams: any;
    @Input() stationId: any;
    @Input() stationName: any;
    @Input() devTypeId: any;
    @Input() devTypeName: any;
    @Input() devNameText: any;
    @Input() alarmTypeName: any;
    @Input() faultTime: any;
    @Input() processList: any;
    @Input() theme: any;
    @Output() private clickCallBack = new EventEmitter();

    triggerTemplate = null;
    czlcVisible = false;
    isCollapsed = false;
    reportFormVisible = false;
    reportFormList: any = [];
    reportFormWidth = document.body.clientWidth - 100;
    czlcList: any = [];
    czlcTitle = '';
    czlcIndex: any = '';
    czlcChildIndex: any = '';
    czlcChildId: any = '';
    czlcData: any = [];
    czlcChildNum = 0;
    czlcStorage: any = [];
    nodata = false;
    popClass = '';
    screenBottom: any = { 'top': '1000px' };
    maskStyle = { 'background-color': 'rgba(0, 0, 0, 0.15)' };

    constructor(
        private http: _HttpClient,
        private datePipe: DatePipe,
        public msg: NzMessageService,
        public ls: LocalStorage
    ) { }

    ngOnInit() {
        if (this.queryParams) {
            const czlcStorage = JSON.parse(this.ls.get('czlcStorage'));
            if (czlcStorage) {
                this.czlcStorage = czlcStorage;
            }
            this.getInfo(this.queryParams.ALARMDESCRIPTION);
        }
        switch (this.theme) {
            case 'blacktheme':
                this.popClass = 'processWrap';
                break;
            case 'whitetheme':
                this.popClass = '';
                break;
        }
    }

    getInfo(alarmId) {
        this.disProcess(alarmId).subscribe((res: any) => {
            if (!res.length) {
                this.nodata = true;
            } else {
                let count = 0;
                for (const item of res) {
                    if (item.children) {
                        for (const list of item.children) {
                            this.reportFormList[count] = {}
                            this.reportFormList[count].kkxd = item.text;
                            this.reportFormList[count].cId = this.queryParams.ALARMDESCRIPTION;
                            this.reportFormList[count].classId = list.classid;
                            this.reportFormList[count].kknr = list.text;
                            this.reportFormList[count].id = list.id;
                            this.reportFormList[count].kkyq = list.main[0].content;
                            this.reportFormList[count].zysj = list.main[1].content;
                            this.reportFormList[count].bz = list.main[2].content;
                            count++;
                            const tempHead = [];
                            const tempMain = [];
                            list.head.forEach((data, index) => {
                                if (index === 2) {
                                    tempHead.push({ width: '8%', name: '开始' }, { width: '8%', name: '完成' }, data);
                                    tempMain.push({ content: '', type: 'time', status: false, name: 'start' }, { content: '', type: 'time', status: false, name: 'end' }, list.main[index]);
                                } else {
                                    tempHead.push(data)
                                    tempMain.push(list.main[index])
                                }
                            });
                            list.head = tempHead;
                            list.main = tempMain;
                        }
                    }

                };
                this.czlcList = res;
                // 默认从第一条开始
                if (this.czlcList) {
                    this.czlcList[0].type = '1';
                }
            }
        });
    }

    // 处置流程内容
    disProcess(id) {
        id = !id ? '' : id;
        const url = environment.apiBase + '/csmis/emProcess/getByCid.jwt?cid=' + id;
        return this.http.get(url);
    }

    czlcOk() {
        if (!this.czlcChildId) {
            this.czlcList[this.czlcIndex].type = '2';
            if (this.czlcIndex + 1 != this.czlcList.length && this.czlcList[this.czlcIndex + 1].type != 2) {
                this.czlcList[this.czlcIndex + 1].type = '1';
            }
        } else {
            this.czlcList[this.czlcIndex].children[this.czlcChildIndex].type = '2';
            this.czlcChildId = '';
            for (const i in this.czlcList[this.czlcIndex].children) {
                if (this.czlcList[this.czlcIndex].children[i].type == '2') {
                    this.czlcChildNum = this.czlcChildNum + 1;
                    if (this.czlcList[this.czlcIndex].children.length == this.czlcChildNum) {
                        this.czlcList[this.czlcIndex].type = '2';
                        this.czlcChildNum = 0;
                    }
                    break;
                }
            }
        }
        this.czlcVisible = false;
    }
    czlcCancel() {
        this.czlcChildId = '';
        this.czlcVisible = false;
    }
    lctClick(index, id, childid, classid, childindex) {
        this.screenBottom = { 'top': document.body.clientHeight - 300 + 'px' };
        this.czlcIndex = Number(index);
        this.czlcChildIndex = Number(childindex);
        this.czlcChildId = Number(childid);
        const item = this.czlcList[this.czlcIndex].children[this.czlcChildIndex];
        this.czlcTitle = item.text;
        // 还原storage数据
        for (const data of this.czlcStorage) {
            if (data.cId === this.queryParams.ALARMDESCRIPTION && data.classId === item.classid && data.id === item.id) {
                for (const list of item.main) {
                    if (data.startTime && list.name === 'start') {
                        list.content = data.startTime;
                        list.status = true;
                    }
                    if (data.endTime && list.name === 'end') {
                        list.content = data.endTime;
                        list.status = true;
                    }
                }
            }
        }

        this.getTemplate(this.stationId, this.devNameText, classid).subscribe((res: any) => {
            // 具体流程数据
            for (const data of this.czlcList) {
                if (data.id == id) {
                    for (const list of data.children) {
                        if (list.id == childid) {
                            this.czlcData.head = list.head;
                            this.czlcData.main = list.main;
                            break;
                        }
                    }
                    break;
                }
            }
            if (classid) {
                for (const data of this.czlcData.main) {
                    if (data.type === 'textarea') {
                        data.content = res.template;
                    }
                }
            }
            this.czlcVisible = true;
        });
        this.clickCallBack.emit(this.czlcTitle);
    }
    timeSwitch(e, name) {
        const item = this.czlcList[this.czlcIndex].children[this.czlcChildIndex];
        const tempArr: any = { cId: this.queryParams.ALARMDESCRIPTION, classId: item.classid, id: item.id, startTime: '', endTime: '' };

        if (e) {
            if (name === 'start') {
                tempArr.startTime = this.datePipe.transform(new Date(), 'HH:mm:ss');
                for (const list of item.main) {
                    if (list.name === 'start') {
                        list.content = tempArr.startTime;
                    }
                }
            }
            if (name === 'end') {
                tempArr.endTime = this.datePipe.transform(new Date(), 'HH:mm:ss');
                for (const list of item.main) {
                    if (list.name === 'end') {
                        list.content = tempArr.endTime;
                    }
                }
            }
        } else {
            if (name === 'start') {
                tempArr.startTime = '';
                for (const list of item.main) {
                    if (list.name === 'start') {
                        list.content = '';
                    }
                }
            }
            if (name === 'end') {
                tempArr.endTime = '';
                for (const list of item.main) {
                    if (list.name === 'end') {
                        list.content = '';
                    }
                }
            }
        }
        if (!this.czlcStorage.length) {
            this.czlcStorage.push(tempArr);
        } else {
            let addData = true;
            for (const data of this.czlcStorage) {
                if (data.cId === this.queryParams.ALARMDESCRIPTION && data.classId === item.classid && data.id === item.id) {
                    if (e) {
                        if (name === 'start') {
                            data.startTime = tempArr.startTime;
                        }
                        if (name === 'end') {
                            data.endTime = tempArr.endTime;
                        }
                    } else {
                        if (name === 'start') {
                            data.startTime = '';
                        }
                        if (name === 'end') {
                            data.endTime = '';
                        }
                    }
                    addData = false;
                } else {
                    addData = true;
                }
            }
            if (addData) {
                this.czlcStorage.push(tempArr);
            }
        }
        for (let i = this.czlcStorage.length - 1; i >= 0; i--) {
            if (!this.czlcStorage[i].startTime && !this.czlcStorage[i].endTime) {
                this.czlcStorage.splice(i, 1)
            }
        }
        this.ls.set('czlcStorage', JSON.stringify(this.czlcStorage));
    }
    // 流程备注
    getTemplate(statid, devname, classid) {
        statid = !statid ? '' : statid;
        devname = !devname ? '' : devname;
        classid = !classid ? '' : classid;

        const url = environment.apiBase + '/csmis/emt/getTemplate.jwt?station=' + statid + '&equipName=' + devname + '&index=' + classid;
        return this.http.get(url);
    }
    navClick(index) {
        this.czlcList[index].childshow = !this.czlcList[index].childshow;
    }
    reportForm() {
        this.reportFormVisible = true;
        for (const item of this.czlcStorage) {
            for (const list of this.reportFormList) {
                if (item.cId === list.cId && item.classId === list.classId && item.id === list.id) {
                    if (item.startTime) {
                        list.xysj = item.startTime;
                    }
                    if (item.endTime) {
                        list.lssj = item.endTime;
                    }
                }
            }
        }
    }
    reportFormOk() {
        const param: any = {};

        param.procedureId = this.queryParams.ALARMDESCRIPTION;
        for (const item of this.processList) {
            if (item.value === this.queryParams.ALARMDESCRIPTION) {
                param.procedureName = item.name;
                break;
            }
        }
        param.faultStationId = this.stationId;
        param.faultStationName = this.stationName;
        param.devTypeId = this.devTypeId;
        param.devTypeName = this.devTypeName;
        param.devName = this.devNameText;
        param.faultDesc = this.alarmTypeName;
        param.faultTime = this.faultTime;
        param.procedures = [];
        for (const item of this.reportFormList) {
            param.procedures.push({
                norder: item.id,
                item: !item.kkxd ? '' : item.kkxd,
                content: !item.kknr ? '' : item.kknr,
                require: !item.kkyq ? '' : item.kkyq,
                startTime: !item.xysj ? '' : item.xysj,
                endTime: !item.lssj ? '' : item.lssj,
                actionInfo: !item.lsqk ? '' : item.lsqk,
                remark: !item.bz ? '' : item.bz
            })
        } 
        this.http.post(environment.project + '/emerFault/add', param).subscribe((res: any) => {
            if (!res.code) {
                this.reportFormVisible = false;
            } else {
                this.msg.error(res.msg);
            }
        });
    }
    reportFormCancel() {
        this.reportFormVisible = false;
    }
}
